<template>
  <div class="v-charts-demo-wrapper">
    <div class="component-example">
      <label class="example-title">使用示例</label>
      <div class="example" v-show="chartOption.length != 0">
        <div v-for="(item,i) in chartOption" class="chart-box" :key="item.theme">
          <h5 class="prop-name" v-if="item.theme != 'selfTheme'">{{item.theme}}主题</h5>
          <h5 class="prop-name" v-else>自定义主题</h5>
          <div v-chart="item" class="chart" :ref="'chartRef'+i" style="width:380px;height:280px;"></div>
        </div>
        <div class="chart-box" >
          <h5 class="prop-3d-name" >3D图表</h5>
          <div class="example-3d-chart chart" v-chart="chartDemo" style="width:380px;height:280px;"></div>
        </div>
        <div class="chart-box" >
          <h5 class="prop-3d-name" >3D图表</h5>
          <div class="example-3d-chart chart" v-chart="chart2DDemo" style="width:380px;height:280px;"></div>
        </div>
      </div>
    </div>
    <div class="component-document">
      <div class="import-property">
        <label class="prop-title">指令输入配置项</label>
        <ul class="prop-lists">
          <li class="prop-item" v-for="(item,index) in props" :key="index">
            <label class="prop-name">{{item.name}}：</label>
            <div class="prop-desc">
              <span> {{item.desc}}</span>
              <div class="sub-element-lists" v-if="item.lists && item.lists.length != 0">
                <ul class="sub-lists">
                  <li class="sub-item" v-for="(op,i) in item.lists" :key="i">
                    <label class="sub-prop-name">{{op.name}}：</label>
                    <span class="sub-prop-value">{{op.desc}}</span>
                  </li>
                </ul>
              </div>
            </div>
          </li>
        </ul>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "VChartsDemo",
  data() {
    return {
      chartOption: [],
      props:[
        {
          name: '指令输入配置项',
          desc: '默认值：{theme:defaultThemeName}，defaultThemeName = macarons。元素数据格式例如：',
          lists: [
            {name: 'option', desc: '绘制图表的option，若为对象，则与echarts官方配置项结构相同；若为数组，则为多个配置项，每个配置项与echarts官方配置项结构相同，指令内部会进行配置项合并'},
            {name: 'theme', desc: '主题名称，默认：undefined'},
            {name: 'themeParam', desc: '主题配置项，默认：undefined'}
          ]
        },
      ],
      methods:[

      ],
      chartDemo:{},
      chart2DDemo:{},
    }
  },
  mounted() {
      this.initOption();
      this.init3DOption();
      this.init3DTo2D();
  },
  methods: {
    initOption() {
      //绘制图表的option
      let option = {
        title: {

        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross',
            label: {
              backgroundColor: '#6a7985'
            }
          }
        },
        legend: {
          data: ['邮件营销', '联盟广告', '视频广告']
        },
        toolbox: {
          feature: {
            saveAsImage: {}
          }
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: [
          {
            type: 'category',
            boundaryGap: false,
            data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
          }],
        yAxis: [
          {
            type: 'value'
          }],
        series: [
          {
            name: '邮件营销',
            type: 'line',
            stack: '总量',
            areaStyle: {},
            data: [120, 132, 101, 134, 90, 230, 210]
          },
          {
            name: '联盟广告',
            type: 'line',
            stack: '总量',
            areaStyle: {},
            data: [220, 182, 191, 234, 290, 330, 310]
          },
          {
            name: '视频广告',
            type: 'line',
            stack: '总量',
            areaStyle: {},
            data: [150, 232, 201, 154, 190, 330, 410]
          }]
      };
      //自定义echarts主题option
      let newThemeOption = {
        "color": [
          "#dc69aa",
          "#07a2a4",
          "#9a7fd1",
          "#588dd5",
          "#f5994e",
          "#c05050",
          "#59678c",
          "#c9ab00",
          "#7eb00a",
          "#6f5553",
          "#2ec7c9",
          "#b6a2de",
          "#5ab1ef",
          "#ffb980",
          "#d87a80",
          "#8d98b3",
          "#e5cf0d",
          "#97b552",
          "#95706d",
          "#c14089"
        ],
        "backgroundColor": "rgba(50,50,50,1)",
        "textStyle": {},
        "title": {
          "textStyle": {
            "color": "#008acd"
          },
          "subtextStyle": {
            "color": "#aaaaaa"
          }
        },
        "line": {
          "itemStyle": {
            "normal": {
              "borderWidth": 1
            }
          },
          "lineStyle": {
            "normal": {
              "width": 2
            }
          },
          "symbolSize": 3,
          "symbol": "emptyCircle",
          "smooth": true
        },
        "radar": {
          "itemStyle": {
            "normal": {
              "borderWidth": 1
            }
          },
          "lineStyle": {
            "normal": {
              "width": 2
            }
          },
          "symbolSize": 3,
          "symbol": "emptyCircle",
          "smooth": true
        },
        "bar": {
          "itemStyle": {
            "normal": {
              "barBorderWidth": 0,
              "barBorderColor": "#ccc"
            },
            "emphasis": {
              "barBorderWidth": 0,
              "barBorderColor": "#ccc"
            }
          }
        },
        "pie": {
          "itemStyle": {
            "normal": {
              "borderWidth": 0,
              "borderColor": "#ccc"
            },
            "emphasis": {
              "borderWidth": 0,
              "borderColor": "#ccc"
            }
          }
        },
        "scatter": {
          "itemStyle": {
            "normal": {
              "borderWidth": 0,
              "borderColor": "#ccc"
            },
            "emphasis": {
              "borderWidth": 0,
              "borderColor": "#ccc"
            }
          }
        },
        "boxplot": {
          "itemStyle": {
            "normal": {
              "borderWidth": 0,
              "borderColor": "#ccc"
            },
            "emphasis": {
              "borderWidth": 0,
              "borderColor": "#ccc"
            }
          }
        },
        "parallel": {
          "itemStyle": {
            "normal": {
              "borderWidth": 0,
              "borderColor": "#ccc"
            },
            "emphasis": {
              "borderWidth": 0,
              "borderColor": "#ccc"
            }
          }
        },
        "sankey": {
          "itemStyle": {
            "normal": {
              "borderWidth": 0,
              "borderColor": "#ccc"
            },
            "emphasis": {
              "borderWidth": 0,
              "borderColor": "#ccc"
            }
          }
        },
        "funnel": {
          "itemStyle": {
            "normal": {
              "borderWidth": 0,
              "borderColor": "#ccc"
            },
            "emphasis": {
              "borderWidth": 0,
              "borderColor": "#ccc"
            }
          }
        },
        "gauge": {
          "itemStyle": {
            "normal": {
              "borderWidth": 0,
              "borderColor": "#ccc"
            },
            "emphasis": {
              "borderWidth": 0,
              "borderColor": "#ccc"
            }
          }
        },
        "candlestick": {
          "itemStyle": {
            "normal": {
              "color": "#d87a80",
              "color0": "#2ec7c9",
              "borderColor": "#d87a80",
              "borderColor0": "#2ec7c9",
              "borderWidth": 1
            }
          }
        },
        "graph": {
          "itemStyle": {
            "normal": {
              "borderWidth": 0,
              "borderColor": "#ccc"
            }
          },
          "lineStyle": {
            "normal": {
              "width": 1,
              "color": "#aaaaaa"
            }
          },
          "symbolSize": 3,
          "symbol": "emptyCircle",
          "smooth": true,
          "color": [
            "#2ec7c9",
            "#b6a2de",
            "#5ab1ef",
            "#ffb980",
            "#d87a80",
            "#8d98b3",
            "#e5cf0d",
            "#97b552",
            "#95706d",
            "#dc69aa",
            "#07a2a4",
            "#9a7fd1",
            "#588dd5",
            "#f5994e",
            "#c05050",
            "#59678c",
            "#c9ab00",
            "#7eb00a",
            "#6f5553",
            "#c14089"
          ],
          "label": {
            "normal": {
              "textStyle": {
                "color": "#eeeeee"
              }
            }
          }
        },
        "map": {
          "itemStyle": {
            "normal": {
              "areaColor": "#dddddd",
              "borderColor": "#eeeeee",
              "borderWidth": 0.5
            },
            "emphasis": {
              "areaColor": "rgba(254,153,78,1)",
              "borderColor": "#444444",
              "borderWidth": 1
            }
          },
          "label": {
            "normal": {
              "textStyle": {
                "color": "#d87a80"
              }
            },
            "emphasis": {
              "textStyle": {
                "color": "rgb(100,0,0)"
              }
            }
          }
        },
        "geo": {
          "itemStyle": {
            "normal": {
              "areaColor": "#dddddd",
              "borderColor": "#eeeeee",
              "borderWidth": 0.5
            },
            "emphasis": {
              "areaColor": "rgba(254,153,78,1)",
              "borderColor": "#444444",
              "borderWidth": 1
            }
          },
          "label": {
            "normal": {
              "textStyle": {
                "color": "#d87a80"
              }
            },
            "emphasis": {
              "textStyle": {
                "color": "rgb(100,0,0)"
              }
            }
          }
        },
        "categoryAxis": {
          "axisLine": {
            "show": true,
            "lineStyle": {
              "color": "#008acd"
            }
          },
          "axisTick": {
            "show": true,
            "lineStyle": {
              "color": "#333"
            }
          },
          "axisLabel": {
            "show": true,
            "textStyle": {
              "color": "#afafaf"
            }
          },
          "splitLine": {
            "show": false,
            "lineStyle": {
              "color": [
                "#eee"
              ]
            }
          },
          "splitArea": {
            "show": false,
            "areaStyle": {
              "color": [
                "rgba(250,250,250,0.3)",
                "rgba(200,200,200,0.3)"
              ]
            }
          }
        },
        "valueAxis": {
          "axisLine": {
            "show": true,
            "lineStyle": {
              "color": "#008acd"
            }
          },
          "axisTick": {
            "show": true,
            "lineStyle": {
              "color": "#333"
            }
          },
          "axisLabel": {
            "show": true,
            "textStyle": {
              "color": "#afafaf"
            }
          },
          "splitLine": {
            "show": true,
            "lineStyle": {
              "color": [
                "#eee"
              ]
            }
          },
          "splitArea": {
            "show": true,
            "areaStyle": {
              "color": [
                "rgba(250,250,250,0.3)",
                "rgba(200,200,200,0.3)"
              ]
            }
          }
        },
        "logAxis": {
          "axisLine": {
            "show": true,
            "lineStyle": {
              "color": "#008acd"
            }
          },
          "axisTick": {
            "show": true,
            "lineStyle": {
              "color": "#333"
            }
          },
          "axisLabel": {
            "show": true,
            "textStyle": {
              "color": "#afafaf"
            }
          },
          "splitLine": {
            "show": true,
            "lineStyle": {
              "color": [
                "#eee"
              ]
            }
          },
          "splitArea": {
            "show": true,
            "areaStyle": {
              "color": [
                "rgba(250,250,250,0.3)",
                "rgba(200,200,200,0.3)"
              ]
            }
          }
        },
        "timeAxis": {
          "axisLine": {
            "show": true,
            "lineStyle": {
              "color": "#008acd"
            }
          },
          "axisTick": {
            "show": true,
            "lineStyle": {
              "color": "#333"
            }
          },
          "axisLabel": {
            "show": true,
            "textStyle": {
              "color": "#afafaf"
            }
          },
          "splitLine": {
            "show": true,
            "lineStyle": {
              "color": [
                "#eee"
              ]
            }
          },
          "splitArea": {
            "show": false,
            "areaStyle": {
              "color": [
                "rgba(250,250,250,0.3)",
                "rgba(200,200,200,0.3)"
              ]
            }
          }
        },
        "toolbox": {
          "iconStyle": {
            "normal": {
              "borderColor": "#2ec7c9"
            },
            "emphasis": {
              "borderColor": "#18a4a6"
            }
          }
        },
        "legend": {
          "textStyle": {
            "color": "#fff"
          }
        },
        "tooltip": {
          "axisPointer": {
            "lineStyle": {
              "color": "#008acd",
              "width": "1"
            },
            "crossStyle": {
              "color": "#008acd",
              "width": "1"
            }
          }
        },
        "timeline": {
          "lineStyle": {
            "color": "#008acd",
            "width": 1
          },
          "itemStyle": {
            "normal": {
              "color": "#008acd",
              "borderWidth": 1
            },
            "emphasis": {
              "color": "#a9334c"
            }
          },
          "controlStyle": {
            "normal": {
              "color": "#008acd",
              "borderColor": "#008acd",
              "borderWidth": 0.5
            },
            "emphasis": {
              "color": "#008acd",
              "borderColor": "#008acd",
              "borderWidth": 0.5
            }
          },
          "checkpointStyle": {
            "color": "#2ec7c9",
            "borderColor": "rgba(46,199,201,0.4)"
          },
          "label": {
            "normal": {
              "textStyle": {
                "color": "#008acd"
              }
            },
            "emphasis": {
              "textStyle": {
                "color": "#008acd"
              }
            }
          }
        },
        "visualMap": {
          "color": [
            "#5ab1ef",
            "#e0ffff"
          ]
        },
        "dataZoom": {
          "backgroundColor": "rgba(47,69,84,0)",
          "dataBackgroundColor": "rgba(239,239,255,1)",
          "fillerColor": "rgba(182,162,222,0.2)",
          "handleColor": "#008acd",
          "handleSize": "100%",
          "textStyle": {
            "color": "#333333"
          }
        },
        "markPoint": {
          "label": {
            "normal": {
              "textStyle": {
                "color": "#eeeeee"
              }
            },
            "emphasis": {
              "textStyle": {
                "color": "#eeeeee"
              }
            }
          }
        }
      }
      this.chartOption = [
        {theme:'wonderland',option},
        {theme:'vintage',option},
        {theme:'dark',option},
        {theme:'westeros',option},
        {theme:'essos',option},
        {theme:'walden',option},
        {theme:'chalk',option},
        {theme:'infographic',option},
        {theme:'macarons',option},
        {theme:'selfTheme',option,themeParam:{themeName:'myTheme',themeOption:newThemeOption}},
      ];
        this.$nextTick(()=>{
        let instance = this.echarts.getInstanceByDom(this.$refs.chartRef0[0]);
        if(instance != undefined){
          instance.on('click',function () {
            console.log('$nextTick else');
          })
          return ;
        }else{
          let timer = setTimeout(()=>{
            let instance = this.echarts.getInstanceByDom(this.$refs.chartRef0);
            instance.on('click',function () {
              console.log('$nextTick if');
            })
            clearTimeout(timer);
            timer = null;
          },1000);
          return ;
        }
      })
    },
    init3DOption(){
      var hours = ['12a', '1a', '2a', '3a', '4a', '5a', '6a',
        '7a', '8a', '9a','10a','11a',
        '12p', '1p', '2p', '3p', '4p', '5p',
        '6p', '7p', '8p', '9p', '10p', '11p'];
      var days = ['Saturday', 'Friday', 'Thursday',
        'Wednesday', 'Tuesday', 'Monday', 'Sunday'];

      var data = [[0,0,5],[0,1,1],[0,2,0],[0,3,0],[0,4,0],[0,5,0],[0,6,0],[0,7,0],[0,8,0],[0,9,0],[0,10,0],[0,11,2],[0,12,4],[0,13,1],[0,14,1],[0,15,3],[0,16,4],[0,17,6],[0,18,4],[0,19,4],[0,20,3],[0,21,3],[0,22,2],[0,23,5],[1,0,7],[1,1,0],[1,2,0],[1,3,0],[1,4,0],[1,5,0],[1,6,0],[1,7,0],[1,8,0],[1,9,0],[1,10,5],[1,11,2],[1,12,2],[1,13,6],[1,14,9],[1,15,11],[1,16,6],[1,17,7],[1,18,8],[1,19,12],[1,20,5],[1,21,5],[1,22,7],[1,23,2],[2,0,1],[2,1,1],[2,2,0],[2,3,0],[2,4,0],[2,5,0],[2,6,0],[2,7,0],[2,8,0],[2,9,0],[2,10,3],[2,11,2],[2,12,1],[2,13,9],[2,14,8],[2,15,10],[2,16,6],[2,17,5],[2,18,5],[2,19,5],[2,20,7],[2,21,4],[2,22,2],[2,23,4],[3,0,7],[3,1,3],[3,2,0],[3,3,0],[3,4,0],[3,5,0],[3,6,0],[3,7,0],[3,8,1],[3,9,0],[3,10,5],[3,11,4],[3,12,7],[3,13,14],[3,14,13],[3,15,12],[3,16,9],[3,17,5],[3,18,5],[3,19,10],[3,20,6],[3,21,4],[3,22,4],[3,23,1],[4,0,1],[4,1,3],[4,2,0],[4,3,0],[4,4,0],[4,5,1],[4,6,0],[4,7,0],[4,8,0],[4,9,2],[4,10,4],[4,11,4],[4,12,2],[4,13,4],[4,14,4],[4,15,14],[4,16,12],[4,17,1],[4,18,8],[4,19,5],[4,20,3],[4,21,7],[4,22,3],[4,23,0],[5,0,2],[5,1,1],[5,2,0],[5,3,3],[5,4,0],[5,5,0],[5,6,0],[5,7,0],[5,8,2],[5,9,0],[5,10,4],[5,11,1],[5,12,5],[5,13,10],[5,14,5],[5,15,7],[5,16,11],[5,17,6],[5,18,0],[5,19,5],[5,20,3],[5,21,4],[5,22,2],[5,23,0],[6,0,1],[6,1,0],[6,2,0],[6,3,0],[6,4,0],[6,5,0],[6,6,0],[6,7,0],[6,8,0],[6,9,0],[6,10,1],[6,11,0],[6,12,2],[6,13,1],[6,14,3],[6,15,4],[6,16,0],[6,17,0],[6,18,0],[6,19,0],[6,20,1],[6,21,2],[6,22,2],[6,23,6]];
      let option = {
        tooltip: {},
        visualMap: {
          max: 20,
          inRange: {
            color: ['#313695', '#4575b4', '#74add1', '#abd9e9', '#e0f3f8', '#ffffbf', '#fee090', '#fdae61', '#f46d43', '#d73027', '#a50026']
          }
        },
        xAxis3D: {
          type: 'category',
          data: hours
        },
        yAxis3D: {
          type: 'category',
          data: days
        },
        zAxis3D: {
          type: 'value'
        },
        grid3D: {
          boxWidth: 160,
          boxHeight: 80,
          boxDepth: 80,
          viewControl: {
            // projection: 'orthographic'
          },
          light: {
            main: {
              intensity: 1.2,
              shadow: true
            },
            ambient: {
              intensity: 0.3
            }
          }
        },
        series: [{
          type: 'bar3D',
          data: data.map(function (item) {
            return {
              value: [item[1], item[0], item[2]],
            }
          }),
          shading: 'lambert',

          label: {
            fontSize: 16,
            borderWidth: 1
          },

          emphasis: {
            label: {
              fontSize: 20,
              color: '#900'
            },
            itemStyle: {
              color: '#900'
            }
          }
        }]
      }
      this.chartDemo = {option};


    },
    init3DTo2D(){
      var option = {
        color: ['#7cb5ec', '#434348', '#90ed7d', '#f7a35c', '#8085e9', '#f15c80', '#e4d354', '#8085e8', '#8d4653', '#91e8e1'],
        title: {
          text: '3D堆叠柱状图',
          x: 'center'
        },
        tooltip: {},
        xAxis3D: {
          type: 'category',
          data: ['1', '2','3', '4','5', '6'],
          axisLine:{
            lineStyle:{
              color:'#333',
              width:2
            }
          },
        },
        yAxis3D: {
          type: 'category',
          data: [''],
          axisLine:{
            lineStyle:{
              color:'#333',
              width:2
            }
          },
        },
        zAxis3D: {
          type: 'value',
          axisLine:{
            lineStyle:{
              color:'#333',
              width:2
            }
          },
        },
        grid3D: {
          boxWidth: 200,
          boxDepth: 20,
          axisPointer: {
            show: false
          },
          light: {
            main: {
              intensity: 1.2
            },
            ambient: {
              intensity: 0.3
            }
          },
          viewControl: {
            /*alpha: 10, //控制场景平移旋转
            beta: 20,
            minAlpha: 10,
            maxAlpha: 10,
            minBeta: 20,
            maxBeta: 20*/
          }
        },
        series: [{
          type: 'bar3D',
          name:'1',
          barSize: 15,
          data: [
            [0, 0, 1200+1200+1800],
            [2, 0, 10000],
            [4, 0, 6000],
          ],
          stack: 'stack',
          shading: 'lambert',
          emphasis: {
            label: {
              show: true
            }
          }
        }, {
          type: 'bar3D',
          name:'2',
          barSize: 15,
          data: [
            [1, 0, 12000],
            [3, 0, 60000],
            [5, 0, 12000],
          ],
          // stack: 'stack',
          shading: 'lambert',
          emphasis: {
            label: {
              show: true
            }
          }
        }]
      };
      this.chart2DDemo = {option};
    },
  }
}
</script>

<style lang="scss" scoped>
.v-charts-demo-wrapper {
  width: 100%;
  height: 100%;
  .component-example{
    width:calc(100% - 350px);
  }
  .component-document{
    width:350px;
  }

  .example{
    display:flex ;
    flex-wrap: wrap;
    .prop-name{
      background: linear-gradient(to right, #00edff, #829cec);
      padding:5px;
    }
  }
  .chart-box{
    width:400px;
    padding:10px;
    height:300px;
    margin:20px 0;
  }
  .prop-3d-name{
    background: linear-gradient(to right, #79ff49, #65ec8a);
    padding:5px;
  }
}
</style>
